﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlNumber : SenseControl
    {
        private int _height;
        private bool _down;
        private bool _increase;
        private int _oldValue;
        private int _numbWidth;

        public int Value { get; set; }
        public int MaxValue = int.MaxValue;
        public int MinValue = int.MinValue;

        public SenseControlNumber()
        {
            Style = new XFItemStyle
                    {
                        TextFont = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Regular),
                        TextColor = Color.Black,
                        Padding = 15,
                        PaddingTop = 18
                    };

            ItemRectangle = new Rectangle();
            _height = -1;
            ItemType = XFItemType.Clickable;
        }

        public override int GetHeight()
        {
            if (_height > -1)
                return _height;

            _numbWidth = Style.TextFont.MeasureString("0", new Rectangle(0, 0, 100, 0)).Width;
            ItemRectangle = this.GetImageRec("counter-two");
            _height = ItemRectangle.Height;
            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int number = 100;
            int iterations = 0;

            while (number < Value)
            {
                iterations++;
                number *= 10;
            }

            int centWidth = (iterations * _numbWidth);

            var leftRec = new Rectangle(x - centWidth, y, ItemRectangle.Width >> 1, ItemRectangle.Height);
            var centRec = new Rectangle(leftRec.X + leftRec.Width, y, centWidth, ItemRectangle.Height);
            var rightRec = new Rectangle(centRec.X + centWidth, y, ItemRectangle.Width >> 1, ItemRectangle.Height);

            DrawUI("counter-left", g, leftRec);
            DrawUI("counter-center", g, centRec,false);
            DrawUI("counter-right", g, rightRec);

            using (SolidBrush b = new SolidBrush(Style.TextColor))
                g.DrawString(Value.ToString(), Style.TextFont, b, leftRec.X + Style.PaddingLeft, y + Style.PaddingTop);
        }

        public override XFItemClickResult GetClickResult()
        {
            return XFItemClickResult.Nothing;
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);
            _oldValue = Value;
            _down = true;
            int half = GetHeight() / 2;

            _increase = y < half;

            while (_down)
            {
                if ((_increase && Value + 1 > MaxValue) || (!_increase && Value - 1 < MinValue))
                    return;

                Value += _increase ? 1 : -1;

                if (Value >= 10)
                    Style.PaddingLeft = 10;
                else if (Value >= 100)
                    Style.PaddingLeft = 0;
                else
                    Style.PaddingLeft = 15;

                Invalidate();

                if (Owner != null)
                {
                    Owner.Invalidate();
                    Owner.Parent.Invalidate();
                }

                Thread.Sleep(200);
                Application.DoEvents();
            }
        }

        public override void OnChange()
        {
            base.OnChange();
            if (Owner != null)
                Owner.OnChange();
        }

        public override void OnBlur()
        {
            base.OnBlur();
            _down = false;
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            int half = GetHeight() / 2;

            _increase = y < half;
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();
            _down = false;

            if (_oldValue != Value)
                OnChange();
        }
    }
}
